package org.i1510.wuyo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.service.impl.BaseServiceImpl;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.wuyo.dao.ICoverTempDao;
import org.i1510.wuyo.po.CoverTemp;
import org.i1510.wuyo.service.ICoverTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iCoverTempService")
public class CoverTempServiceImpl extends BaseServiceImpl implements
		ICoverTempService {
	@Autowired
	private ICoverTempDao iCoverTempDao;

	public ResultMapper save(CoverTemp basePo, HttpServletRequest request) {

		ResultMapper mapper = ResultMapper.generateFailure();
		CoverTemp saved = null;
		if (null != basePo && basePo.getDocId() != null && !basePo.getDocId().equals("")) {// 修改
			CoverTemp finder = iCoverTempDao.findById(basePo.getDocId());
			if (null != finder) {
				finder.setDocLastTime(DateUtils.getNow());
				finder.setDocLastor(UserUtils.getSessionUserBean(request)
						.getSysUser().getDocId());
				BeanUtils.copy(finder, basePo);
				saved = iCoverTempDao.save(finder);
			} else {
				mapper = ResultMapper.generateFailure();
				return mapper;
			}

		} else {// 新增
			basePo.setDocId(IDGenerator.getGenerateID());
			basePo.setDocCreateTime(DateUtils.getNow());
			basePo.setDocCreator(UserUtils.getSessionUserBean(request)
					.getSysUser().getDocId());
			saved = iCoverTempDao.save(basePo);
		}

		if (saved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		} else {
			mapper = ResultMapper.generateSuccess();
			return mapper;
		}
	}

	public ResultMapper delete(String docId) {

		ResultMapper mapper = ResultMapper.generateFailure();
		iCoverTempDao.delete(docId);
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}

	public CoverTemp findById(String docId) {
		return iCoverTempDao.findById(docId);
	}

	public List<CoverTemp> findByIds(List<String> docIds) {
		return (List<CoverTemp>) iCoverTempDao.findAll(docIds);
	}

	public List getPage(CoverTemp coverTemp, PageBean pageBean, Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(coverTemp, map);
		map.put("intPageIndex", pageBean.getIntPageIndex());
		map.put("intPageSize", pageBean.getIntPageSize());
		map.putAll(conditions);
		List list = this.queryForListBySql("coverTempSql.getPage", map);
		return list;
	}

	public int getPageCount(CoverTemp coverTemp, Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(coverTemp, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("coverTempSql.getPageCount", map);
		return list.size();
	}

}
